﻿using System;
using System.Text;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.SilverlightControls;
using System.Drawing;
using System.Collections.Generic;

using SBPweb.Forge.Cryptography;

[assembly:TagPrefix("SBPweb.Forge.Controls.Web", "SBPweb")]
namespace SBPweb.Forge.Controls.Web
{

    public partial class SilverlightContainer : Silverlight
    {

        #region Private fields

        #endregion

        #region Constructor logic

        public SilverlightContainer()
        {
            InitializeComponent();
            InitializeHandlers();
            InitializeParameters();
        }

        private void InitializeHandlers()
        {
        }

        private void InitializeParameters()
        {
            string[] pars = new string[]{
                string.Format("SessionId={0}", Forge.Web.SessionManager.Session.SessionIdentifier),
                string.Format("ClientIp={0}", IPAddress.Parse(HttpContext.Current.Request.UserHostAddress).ToString()),
                string.Format("ServerProcessedTime={0}", DateTime.Now.ToFileTimeUtc())
            };
            StringBuilder sb = new StringBuilder();
            foreach(string p in pars)
            {
                if(sb.Length > 0)
                {
                    sb.Append(",");
                }
                sb.Append(p);
            }
            InitParameters = sb.ToString();
        }

        #endregion

        #region Property accessors

        public bool EnableGPUAcceleration
        {
            get;
            set;
        }

        public bool EnableInitializationParametersEncryption
        {
            get;
            set;
        }

        public string InitialParametersEncryptionAdditionalSalt
        {
            get;
            set;
        }

        #endregion

        #region Methods

        protected void InitializeComponent()
        {
            this.Width = new Unit("100%");
            this.Height = new Unit("100%");

            //HtmlGenericControl obj = new HtmlGenericControl("object");
            //HtmlGenericControl iframe = new HtmlGenericControl("iframe");
            
            //this.Controls.Add(obj);
            //this.Controls.Add(iframe);

            //obj.Attributes.Add("data", "data:application/x-silverlight,");
            //obj.Attributes.Add("type", "application/x-silverlight");
            //obj.Attributes.Add("width", "100%");
            //obj.Attributes.Add("height", "100%");
            //obj.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Transparent");

            //obj.Controls.Add(GetObjectParam("source", HttpContext.Current.Server.MapPath(Source)));
            //obj.Controls.Add(GetObjectParam("onerror", "onSilverlightError"));
            //obj.Controls.Add(GetObjectParam("background", "#00FFFFFF"));
            //obj.Controls.Add(GetObjectParam("minRuntimeVersion", "2.0.31005.0"));
            //obj.Controls.Add(GetObjectParam("autoUpgrade", "True"));
            //obj.Controls.Add(GetObjectParam("EnableHtmlAccess", "True"));
            //obj.Controls.Add(GetObjectParam("Windowless", Windowless.ToString()));

            //iframe.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
            //iframe.Style.Add(HtmlTextWriterStyle.Height, "0");
            //iframe.Style.Add(HtmlTextWriterStyle.Width, "0");
            //iframe.Style.Add(HtmlTextWriterStyle.BorderWidth, "0px");

            this.Windowless = true;
            this.HtmlAccess = HtmlAccess.Enabled;
            this.BackColor = Color.Transparent;
            this.PluginBackground = Color.Transparent;
            
            /*
    <div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" style="background-color: Transparent">
			<param name="source" value="ClientBin/SBPweb.WebSite.Presentation.Silverlight.xap"/>
			<param name="onerror" value="onSilverlightError" />
			<param name="background" value="#00FFFFFF" />
			<param name="minRuntimeVersion" value="2.0.31005.0" />
			<param name="autoUpgrade" value="true" />
			<param name="EnableHtmlAccess" value="True" />
			<param name="Windowless" value="True" />
			<a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
     			<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
			</a>
		</object>
		<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
    </div>
            */
        }

        //private HtmlGenericControl GetObjectParam(string name, string value)
        //{
        //    HtmlGenericControl ret = new HtmlGenericControl("param");
        //    ret.Attributes.Add("name", name);
        //    ret.Attributes.Add("value", value);
        //    return ret;
        //}

        protected override IDictionary<string, string> GetSilverlightParameters()
        {
            IDictionary<string, string> ret = base.GetSilverlightParameters();
            if (!ret.ContainsKey("enableGPUAcceleration"))
            {
                ret["enableGPUAcceleration"] = this.EnableGPUAcceleration.ToString().ToLower();
            }
            return ret;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (EnableInitializationParametersEncryption)
            {
                this.InitParameters = EncryptParameters(this.InitParameters);
            }
        }

        private string EncryptParameters(string parameters)
        {
            string pt = string.Format("{0}#{1}", HttpContext.Current.Request.Url, HttpContext.Current.Request.UserAgent);
            if (!string.IsNullOrEmpty(InitialParametersEncryptionAdditionalSalt))
            {
                pt += InitialParametersEncryptionAdditionalSalt;
            }
            CustomCryptographyProvider ccp = new CustomCryptographyProvider();
            return string.Format("{0}={1}", "hash", ccp.Encrypt<string>(parameters, pt));
        }

        #endregion

    }

}
